//
//  MainNavigationController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/24.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class MainNavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //设为黑色不透明
        self.navigationBar.translucent = false
        self.navigationBar.barTintColor = UIColor.blackColor()
        self.settingNavigationBarStyle()
    }
    
    
    //设置navigationBar的总样式
    func settingNavigationBarStyle(){
        let naviStyle = UINavigationBar.appearance()
        //状态栏文字的大小和颜色
        naviStyle.titleTextAttributes = [NSFontAttributeName : BaseFunc.getSystemFont(18*SCALEW), NSForegroundColorAttributeName : Theme.NaviColor]
        //状态栏按钮的颜色和大小
        let item = UIBarButtonItem.appearance()
        item.setTitleTextAttributes([NSFontAttributeName : BaseFunc.getSystemFont(18*SCALEW), NSForegroundColorAttributeName : Theme.NaviColor], forState: UIControlState())
    }
    
    //创建状态栏的返回按钮
    lazy var backButton:UIButton = {
        let backButton = UIButton(frame: CGRect(x: 0,y: 0,width: 55,height: 21))
        backButton.setImage(UIImage(named:"backLight"), forState: UIControlState())
        backButton.setImage(UIImage(named:"backLight"), forState: .Highlighted)
        backButton.setTitle("返回", forState: UIControlState())
        backButton.setTitleColor(Theme.NaviColor, forState: UIControlState())
        backButton.addTarget(self, action: #selector(self.backButtonClick(_:)), forControlEvents: .TouchUpInside)
        backButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 7, bottom: 0, right: 0)
        return backButton
    }()
    
    
    //返回按钮点击事件
    func backButtonClick(sender:UIButton){
        self.popViewControllerAnimated(true)
    }
    
    //发生跳转时触发的方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //刚进入app的时候 也算是push了试图  所以判断子试图是不是等于0
        if self.childViewControllers.count>0{
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
            viewController.navigationItem.hidesBackButton = true//原先用标题做返回按钮的 还存在 在这里隐藏掉
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
}


































